package com.itwn.portal.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwn.portal.entity.Resource;
import com.itwn.portal.mapper.BuyMapper;
import com.itwn.portal.mapper.ResourceMapper;
import com.itwn.portal.mapper.UserMapper;
import com.itwn.portal.vo.ResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ResourceService {
    @Autowired
    ResourceMapper resourceMapper;
    @Autowired
    BuyMapper buyMapper;
    @Autowired
    UserMapper userMapper;

    public PageInfo<ResourceVo> initAllResource(Integer now,Integer size,String name, Integer tagId,Integer num,Integer orders){
        PageHelper.startPage(now,size);
        List<ResourceVo> resourceVos = resourceMapper.selectResource(name, tagId,num,orders);
        System.out.println(resourceVos);
        return new PageInfo<>(resourceVos);
    }

    @Cacheable(value = "resource",key = "#root.methodName+#root.args")
    public List<ResourceVo> selectByFine(Integer num) {
        List<ResourceVo> resourceVos = resourceMapper.selectByFine(num);
        System.out.println(resourceVos);
        return resourceVos;
    }

    @Cacheable(value = "resource",key = "#root.methodName+#root.args")
    public List<ResourceVo> queryBuy(Integer num) {
        return resourceMapper.selectByBuy(num);
    }

    @Cacheable(value = "resource",key = "#root.methodName+#root.args")
    public List<ResourceVo> queryRed(Integer num) {
        return resourceMapper.selectByRed(num);
    }

    public List<ResourceVo> queryByCons() {
        return resourceMapper.selectByCons();
    }

    @Cacheable(value = "resource",key = "#root.methodName+#root.args")
    public List<ResourceVo> queryMonth(Integer num) {
        return resourceMapper.selectMonth(num);
    }

    @Cacheable(value = "resource",key = "#root.methodName+#root.args")
    public List<ResourceVo> queryCollect(Integer num) {
        return resourceMapper.selectByCollect(num);
    }

    @Cacheable(value = "resource",key = "#root.methodName+#root.args")
    public List<ResourceVo> querySex(Integer num) {
        return resourceMapper.selectBySex(num);
    }

    public List<ResourceVo> queryPersonCollect(Integer uid) {
        return resourceMapper.selectPersonCollect(uid);
    }


    public List<Integer> queryTotalByUid(Integer uid) {
        List<Integer> list = new ArrayList<>();
        Integer BuyTotal = resourceMapper.selectPersonBuyTotalByUid(uid);
        list.add(BuyTotal);
        Integer collectTotal = buyMapper.selectPersonCollectTotalByUid(uid);
        list.add(collectTotal);
        return list;
    }

    @CacheEvict(value="resource",keyGenerator = "myGenerateKey")
    public Resource queryById(Integer num) {
        return resourceMapper.selectByPrimaryKey(num);
    }

    @CacheEvict(value="resource",keyGenerator = "myGenerateKey",allEntries = true)
    @Transactional
    public void voteMTicket(Integer rid,Integer uid) {
        this.userMapper.delMTicket(uid);
        this.resourceMapper.updateIncrMTicketCount(rid);
    }

    @CacheEvict(value="resource",keyGenerator="myGenerateKey",allEntries = true)
    public void voteColor(Integer rid, Integer colornum) {
        if(colornum == 1){
            // 投红票
            this.resourceMapper.voteRed(rid);
        }
        if(colornum == 2){
            // 投黑票
            this.resourceMapper.voteBlack(rid);
        }
    }


    public int queryFirstSectionId(Integer rid) {

        return this.resourceMapper.selectFirstSectionId(rid);
    }
}
